#ifndef ReferenceCounted_h
#define ReferenceCounted_h

#include <assert.h>

namespace hxe
{
    class ReferenceCounted
    {
    public:
        ReferenceCounted():referenceCounter(0),debugName(0)
        {
            id = ++increaseId;
            grab();
        }

        virtual ~ReferenceCounted()
        {
        }

        void grab()
        {
            ++referenceCounter;
            ++totalReferenceCounter;
        }

        bool drop()
        {
            --referenceCounter;
            --totalReferenceCounter;
            assert(referenceCounter >= 0);
            if (!referenceCounter)
            {
                delete this;
                return true;
            }

            return false;
        }

        int getReferenceCount() const
        {
            return referenceCounter;
        }

        const char* getDebugName() const
        {
            return debugName;
        }

        int getId() const
        {
            return id;
        }

    protected:
        void setDebugName(const char* name)
        {
            debugName = name;
        }

    private:
        const char* debugName;
        int referenceCounter;
        int id;


    public:
        static int getTotalReferenceCounter()
        {
            return totalReferenceCounter;
        }

    private:
        static int totalReferenceCounter;
        static int increaseId;
    };
}

#endif // ReferenceCounted_h
